/**
 * Param.java [V 1.0.0]
 * classes : com.xiaocq.iwordkiller.Param
 * xiaocq Create at 2012-4-10 下午3:41:46
 */
package com.xiaocq.iwordkiller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

/**
 * com.xiaocq.iwordkiller.Param
 * @author Mystory
 * Create at 2012-4-10 下午3:41:46
 */
public class Param {
	public static final String PARAM_NAME = "params";
	
	public static final boolean CHOICE = false;
	public static final boolean COMPLETION = true;
	public static final int ISUNIT = 51;
	public static final int ALL = 51;
	public static final int UNDO = 52;
	public static final int DO = 53;
	public static final int RIGHT = 54;
	public static final int WRONG = 55;
	
	private static final String TEST_ID = "testId";
	private static final String TEST_TYPE = "testType";
	private static final String TEST_COUNTS = "testCounts";
	private static final String FINISHED_COUNTS = "finishedCounts";
	private static final String TOTAL_TEST_TIME = "totalTestTime";
	private static final String FINISH_TIME = "finishTime";
	private static final String IS_NEW_TEST = "isNewTest";
	private static final String WRONG_COUNTS = "wrong_counts";
	
	public boolean isNewTest;
	public int testId;	//测试Id 1-50表示单元测试的前50个单元，51-55表示自主测试的五种方法
	public boolean testType;	//测试类型 true表示填空题，false表示单选题
	public int testCounts;	//测试单词的个数
	public int finishedCounts;	//已完成单词的个数
	public long totalTestTime;	//累计测试用时
	public long finishTime;	//完成时间
	
	private int wrongCounts;
	
	private Context context;
	private SharedPreferences pref;
	
	public Param(Context context){
		this.context = context;
	}
	
	public int getWrongCounts(){
		return wrongCounts;
	}
	
	public void setWrongCounts(int wrongNumber){
		this.wrongCounts = wrongNumber;
	}
	
	
	/**
	 * 从Intent中获取相关参数
	 * @param intent
	 */
	public void loadFromIntent(Intent intent){
		testId = intent.getIntExtra(TEST_ID, 1);
		testType = intent.getBooleanExtra(TEST_TYPE, false);
		testCounts = intent.getIntExtra(TEST_COUNTS, 73);
		finishedCounts = intent.getIntExtra(FINISHED_COUNTS, 0);
		totalTestTime = intent.getLongExtra(TOTAL_TEST_TIME, 0L);
		finishTime = intent.getLongExtra(FINISH_TIME, 0L);
		isNewTest = intent.getBooleanExtra(IS_NEW_TEST, true);
		wrongCounts = intent.getIntExtra(WRONG_COUNTS, 0);
	}
	
	/**
	 * 将相关参数写入intent中
	 * @return
	 */
	public Intent getIntentOfParam(){
		Intent intent = new Intent();
		intent.putExtra(TEST_ID, testId);
		intent.putExtra(TEST_TYPE, testType);
		intent.putExtra(TEST_COUNTS, testCounts);
		intent.putExtra(FINISHED_COUNTS, finishedCounts);
		intent.putExtra(TOTAL_TEST_TIME, totalTestTime);
		intent.putExtra(FINISH_TIME, finishTime);
		intent.putExtra(IS_NEW_TEST, isNewTest);
		intent.putExtra(WRONG_COUNTS, wrongCounts);
		return intent;
	}
	
	/**
	 * 从PARAM_NAME的XML文件中读取保存的参数
	 * 如果是新测试则初始化，否则读取保存数据
	 */
	public void load(){
		pref = context.getSharedPreferences(PARAM_NAME, Context.MODE_PRIVATE);
		
		isNewTest = pref.getBoolean(IS_NEW_TEST, true);
		if(isNewTest){
			return;
		}
		testId = pref.getInt(TEST_ID, 1);
		testType = pref.getBoolean(TEST_TYPE, false);
		testCounts = pref.getInt(TEST_COUNTS, 73);
		finishedCounts = pref.getInt(FINISHED_COUNTS, 0);
		totalTestTime = pref.getLong(TOTAL_TEST_TIME, 0L);
		finishTime = pref.getLong(FINISH_TIME, 0L);
	}
	
	/**
	 * 将参数保存到指定名称的XML文件中
	 * @return
	 */
	public boolean save(){
		pref = context.getSharedPreferences(PARAM_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		
		editor.putInt(TEST_ID, testId);
		editor.putBoolean(TEST_TYPE, testType);
		editor.putInt(TEST_COUNTS, testCounts);
		editor.putInt(FINISHED_COUNTS, finishedCounts);
		editor.putLong(TOTAL_TEST_TIME, totalTestTime);
		editor.putLong(FINISH_TIME, finishTime);
		editor.putBoolean(IS_NEW_TEST, isNewTest);
		
		return editor.commit();
	}
	
	/**
	 * 初始化一个新测试
	 */
	public void initNewParam(){
		isNewTest = true;
		testId = 1;
		testType = false;
		testCounts = 73;
		finishedCounts = 0;
		totalTestTime = 0L;
		finishTime = 0L;
		wrongCounts = 0;
	}

	/**
	 * 判断该参数是否被改变过
	 * @param param
	 * @return
	 */
	public boolean isNoChanged(){
		Param param = new Param(context);
		param.load();
		return this.equals(param);
	}
	
	
	@Override
	public String toString() {
		return "Param [isNewTest=" + isNewTest + ", testId=" + testId
				+ ", testType=" + testType + ", testCounts=" + testCounts
				+ ", finishedCounts=" + finishedCounts + ", totalTestTime="
				+ totalTestTime + ", finishTime=" + finishTime
				+ ", wrongCounts=" + wrongCounts + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (finishTime ^ (finishTime >>> 32));
		result = prime * result + finishedCounts;
		result = prime * result + (isNewTest ? 1231 : 1237);
		result = prime * result + testCounts;
		result = prime * result + testId;
		result = prime * result + (testType ? 1231 : 1237);
		result = prime * result
				+ (int) (totalTestTime ^ (totalTestTime >>> 32));
		result = prime * result + wrongCounts;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Param other = (Param) obj;
		if (finishTime != other.finishTime)
			return false;
		if (finishedCounts != other.finishedCounts)
			return false;
		if (isNewTest != other.isNewTest)
			return false;
		if (testCounts != other.testCounts)
			return false;
		if (testId != other.testId)
			return false;
		if (testType != other.testType)
			return false;
		if (totalTestTime != other.totalTestTime)
			return false;
		if (wrongCounts != other.wrongCounts)
			return false;
		return true;
	}




	public static class StatisticParam{
		public static final String STATISTIC = "statistic";
		
		private static final String TOTAL_WORDS = "totalWords";
		private static final String TOTAL_RIGHT = "totalRight";
		private static final String TOTAL_WRONG = "totalWrong";
		private static final String TOTAL_HIT_RATE = "totalHitRate";
		private static final String TOTAL_TIME_COST = "totalTimeCost";
		private static final String COMPLETION_TEST_COUNT = "completionTestCount";
		private static final String CHOICE_TEST_COUNT = "choiceTestCount";
		private static final String AVE_COMPLETION_TEST_SPEED = "aveCompletionTestSpeed";
		private static final String AVE_CHOICE_TEST_SPEED = "aveChoiceTestSpeed";
		
		public int totalWords;
		public int totalRight;
		public int totalWrong;
		public float totalHitRate;
		public long totalTimeCost;
		public int completionTestCount;
		public int choiceTestCount;
		public float aveCompletionTestSpeed;
		public float aveChoiceTestSpeed;
		
		private Context context;
		private SharedPreferences pref;
		
		public StatisticParam(Context context){
			this.context = context;
			this.pref = context.getSharedPreferences(STATISTIC, Context.MODE_PRIVATE);
		}
		
		public void load(){
			totalWords = pref.getInt(TOTAL_WORDS, 0);
			totalRight = pref.getInt(TOTAL_RIGHT, 0);
			totalWrong = pref.getInt(TOTAL_WRONG, 0);
			totalHitRate = pref.getFloat(TOTAL_HIT_RATE, 0.00f);
			totalTimeCost = pref.getLong(TOTAL_TIME_COST, 0L);
			completionTestCount = pref.getInt(COMPLETION_TEST_COUNT, 0);
			choiceTestCount = pref.getInt(CHOICE_TEST_COUNT, 0);
			aveCompletionTestSpeed = pref.getFloat(AVE_COMPLETION_TEST_SPEED, 0.00f);
			aveChoiceTestSpeed = pref.getFloat(AVE_CHOICE_TEST_SPEED, 0.00f);
		}
		
		public void save(){
			SharedPreferences.Editor editor = pref.edit();
			
			editor.putInt(TOTAL_WORDS, totalWords);
			editor.putInt(TOTAL_RIGHT, totalRight);
			editor.putInt(TOTAL_WRONG, totalWrong);
			editor.putFloat(TOTAL_HIT_RATE, totalHitRate);
			editor.putInt(COMPLETION_TEST_COUNT, completionTestCount);
			editor.putInt(CHOICE_TEST_COUNT, choiceTestCount);
			editor.putLong(TOTAL_TIME_COST, totalTimeCost);
			editor.putFloat(AVE_COMPLETION_TEST_SPEED, aveCompletionTestSpeed);
			editor.putFloat(AVE_CHOICE_TEST_SPEED, aveChoiceTestSpeed);
			
			editor.commit();
		}
		
		public void addParam(Param param, float perWordTimeCost){
			totalWords += param.testCounts;
			totalWrong += param.getWrongCounts();
			totalRight = totalWords - totalWrong;
			totalHitRate = (float) ((totalRight == 0) ? 0.00f : (totalRight * 1.0 / totalWords));
			totalTimeCost += param.totalTestTime;
			if(param.testType){
				completionTestCount++;
				aveCompletionTestSpeed = (float) ((aveCompletionTestSpeed + perWordTimeCost) * 1.0 / completionTestCount);
			}else{
				choiceTestCount++;
				aveChoiceTestSpeed = (float) ((aveChoiceTestSpeed + perWordTimeCost) * 1.0 / choiceTestCount);
			}
		}
		
		public void clear(){
			totalWords = 0;
			totalWrong = 0;
			totalRight = 0;
			totalHitRate = 0.00f;
			totalTimeCost = 0L;
			completionTestCount = 0;
			choiceTestCount = 0;
			aveChoiceTestSpeed = 0.00f;
			aveCompletionTestSpeed = 0.00f;
		}
	}
	
}
